<?php

class baseAction extends Action
{
    protected function _initialize() {
        //消除所有的magic_quotes_gpc转义
        Input::noGPC();
        //初始化网站配置
        if (false === $setting = F('setting')) {
            $setting = D('setting')->setting_cache();
        }
        C($setting);
        //发送邮件
        $this->assign('async_sendmail', session('async_sendmail'));
    }
    
    public function _empty() {
        $this->_404();
    }


    
    protected function _404($url = '') {
        if ($url) {
            redirect($url);
        } else {
            send_http_status(404);
            $this->display(TMPL_PATH . '404.html');
            exit;
        }
    }

    /**
     * 添加邮件到队列
     */
    protected function _mail_queue($to, $subject, $body, $priority = 1) {
        $to_emails = is_array($to) ? $to : array($to);
        $mails = array();
        $time = time();
        foreach ($to_emails as $_email) {
            $mails[] = array(
                'mail_to' => $_email,
                'mail_subject' => $subject,
                'mail_body' => $body,
                'priority' => $priority,
                'add_time' => $time,
                'lock_expiry' => $time,
            );
        }
        M('mail_queue')->addAll($mails);

        //异步发送邮件
        $this->send_mail(false);
    }

    /**
     * 发送邮件
     */
    public function send_mail($is_sync = true) {
        if (!$is_sync) {
            //异步
            session('async_sendmail', true);
            return true;
        } else {
            //同步
            session('async_sendmail', null);
            return D('mail_queue')->send();
        }
    }

    /**
     * 上传文件默认规则定义
     */
    protected function _upload_init($upload) {
        $allow_max = C('pin_attr_allow_size'); //读取配置
        $allow_exts = explode(',', C('pin_attr_allow_exts')); //读取配置
        $allow_max && $upload->maxSize = $allow_max * 1024;   //文件大小限制
        $allow_exts && $upload->allowExts = $allow_exts;  //文件类型限制
        $upload->saveRule = 'uniqid';
        return $upload;
    }

    /**
     * 上传文件
     */
    protected function _upload($file, $dir = '', $thumb = array(), $save_rule='uniqid') {
        $upload = new UploadFile();
        if ($dir) {
            $upload_path = C('pin_attach_path') . $dir . '/';
            $upload->savePath = $upload_path;
        }
        if ($thumb) {
            $upload->thumb = true;
            $upload->thumbMaxWidth = $thumb['width'];
            $upload->thumbMaxHeight = $thumb['height'];
            $upload->thumbPrefix = '';
            $upload->thumbSuffix = isset($thumb['suffix']) ? $thumb['suffix'] : '_thumb';
            $upload->thumbExt = isset($thumb['ext']) ? $thumb['ext'] : '';
            $upload->thumbRemoveOrigin = isset($thumb['remove_origin']) ? true : false;
        }
        //自定义上传规则
        $upload = $this->_upload_init($upload);
        if( $save_rule!='uniqid' ){
            $upload->saveRule = $save_rule;
        }

        if ($result = $upload->uploadOne($file)) {
            return array('error'=>0, 'info'=>$result);
        } else {
            return array('error'=>1, 'info'=>$upload->getErrorMsg());
        }
    }

    /**
     * AJAX返回数据标准
     *
     * @param int $status
     * @param string $msg
     * @param mixed $data
     * @param string $dialog
     */
    protected function ajaxReturn($status=1, $msg='', $data='', $dialog='') {
        parent::ajaxReturn(array(
            'status' => $status,
            'msg' => $msg,
            'data' => $data,
            'dialog' => $dialog,
        ));
    }

    //发送get请求
    public function send_get_req($url){
        // while(1){
            $ch = curl_init();  
            curl_setopt($ch, CURLOPT_URL, $url);  
            curl_setopt($ch, CURLOPT_HEADER, false);  
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);  
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);  
              
            $res = curl_exec($ch);  
            $rescode = curl_getinfo($ch, CURLINFO_HTTP_CODE);   
            curl_close($ch);

            return $res;
    }
    
    //发送post请求
    public function send_post_req($url,$data){                                                         // 模拟提交数据函数
        $curl = curl_init();                                                                          // 启动一个CURL会话
        curl_setopt($curl, CURLOPT_URL, $url);                                                        // 要访问的地址
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);                                            // 对认证证书来源的检查
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);                                            // 从证书中检查SSL加密算法是否存在
        curl_setopt($curl, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)'); // 模拟用户使用的浏览器
        // curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);                                             // 使用自动跳转
        // curl_setopt($curl, CURLOPT_AUTOREFERER, 1);                                                // 自动设置Referer
        curl_setopt($curl, CURLOPT_POST, 1);                                                          // 发送一个常规的Post请求
        curl_setopt($curl, CURLOPT_POSTFIELDS, $data);                                                // Post提交的数据包x
        curl_setopt($curl, CURLOPT_TIMEOUT, 30);                                                      // 设置超时限制防止死循环
        curl_setopt($curl, CURLOPT_HEADER, 0);                                                        // 显示返回的Header区域内容
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);                                                // 获取的信息以文件流的形式返回
        $tmpInfo = curl_exec($curl);                                                                  // 执行操作
        if (curl_errno($curl)) {
           echo 'Errno'.curl_error($curl);                                                            //捕抓异常
        }
        curl_close($curl);                                                                            // 关闭CURL会话
        return $tmpInfo;                                                                              // 返回数据
    }

    //得到商圈信息
    function ajax_shangquan(){
        $qu = $this->_get('qu','');
        if($qu){
            $shangquans = M('address')->where("Area='{$qu}'")->distinct(true)->field('business_area')->select();
        }else{
            $shangquans = M('address')->distinct(true)->field('business_area')->setField('business_area')->select();
        }
        $this->ajaxReturn(1,'商圈信息',$shangquans);
    }
    //得到商圈信息
    function ajax_building(){
        $business_area = $this->_get('business_area','business_area');
        if($business_area){
            $data = M('address')->where("business_area='{$business_area}'")->select();
        }else{
            $data = M('address')->setField('building')->select();
        }
        $this->ajaxReturn(1,'大厦信息',$data);
    }
}